package org.ocean.online.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.apache.wicket.request.Request;
import org.ocean.hibernate.BaseHibernateDao;
import org.ocean.online.constant.Constant;
import org.ocean.online.sys.DbGuard;
import org.ocean.online.sys.DbGuardFactory;

public class UserUtil {

	public static String getWorkspace(String username){
		return getUserDataDir(username)+"webapp"+File.separator;
	}
	
	public static String getFullFilePath(HttpServletRequest request){
		String fullPath ="";
		String path = request.getServletPath();
		String username = UserUtil.getUsernameFromRequest(request);
//		path = path.substring(("/online/"+username).length(), path.length());
		path = path.substring(("/app/"+username).length(), path.length());
		fullPath = UserUtil.getWorkspace(username)+path;
		return fullPath;
		
	}
	
	private static String getUserDataDir(String username){
		Map user = getUser(username);
		if(user==null){
			return "";
		}
		String path= user.get(Constant.Workspace_VariableName)+File.separator;
		return path;
	}
	
	public static String getDbSpace(String username){
		String path = getUserDataDir(username)+"db"+File.separator;
		File file = new File(path);
		if(file.exists()==false){
			file.mkdir();
		}
		return getUserDataDir(username)+"db"+File.separator;
	}
	
	private static Map getUser(String username){
		BaseHibernateDao dao = new BaseHibernateDao();
		return  (Map) dao.findUnique("from user where username=?", new Object[]{username});
	}
	public static DbGuard getDb(String username){
		Map user = new HashMap();
		user.put("username", username);
		return DbGuardFactory.getDbGuard(user);
	}
	
	public static String getUsernameFromRequest(Request req) {
		HttpServletRequest request = ((ServletWebRequest) req).getContainerRequest();
		return getUsernameFromRequest(request);
	}
	
	private static String getUsernameFromRequest(HttpServletRequest request) {
		String path = request.getServletPath();
		String[] arr = path.split("/");
		if(arr.length>2){
			return arr[2];
		}
		return "";
	}
	
	public static void saveFile(String username,String filePath,InputStream fis){
		String userDir =getUserDataDir(username); 
		String workspace = getWorkspace(username);
//		String dbspace = getDbSpace(username);
//		File workspaceDir = new File(workspace);
//		File dbspaceDir = new File(dbspace);
		double size1 = CommonUtil.getDirSize(new File(userDir));
//		double size2 = CommonUtil.getDirSize(dbspaceDir);
		if((size1)>50){
			throw new RuntimeException("not enough space left");
		}
		File file = new  File(workspace+File.separator+filePath);
		try {
			FileOutputStream fos = FileUtils.openOutputStream(file);
			int bufSize=0;
			while((bufSize = fis.available())>0){
				byte[] buffer = new byte[bufSize];
				fis.read(buffer);
				fos.write(buffer);
			}
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
